<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :cols="2" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div style="height: 47px"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right',
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">确 认</multiuse-button>
    </div>
  </div>
</template>
<script>
import { getDealerAddress, confirmAddressAdjust } from '@/api/omSale/OrderManagement/OrderAuditConfirmSideB';
export default {
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      addressList: []
    };
  },
  props: ['list'],
  created() {
    this.getList(this.list);
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '收车地址',
          fieldName: 'addressId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
        },
      ];
    },
    async getList(list) {
      let res = await getDealerAddress({ dealerCode: list[0].dealerCode });
      this.addressList = res.data;
      this.formList.find((x) => x.fieldName == 'addressId').itemList = res.data.map((x) => ({ text: x.address, value: x.addressId }));
    },
    changeHandle(val) {
      this.saveData = val;
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      } else {
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          var deliveryOrderIdList = [];
          //获取选中销售订单id
          for (let i = 0; i < this.list.length; i++) {
            deliveryOrderIdList[i] = this.list[i].id;
          }
          json.address = this.addressList.find(x=>x.addressId == json.addressId).address;
          // json.deliveryCityName = this.addressList.find(x=>x.addressId == json.addressId).cityName;
          json.linkMan = this.addressList.find(x=>x.addressId == json.addressId).linkMan;
          json.linkPhone = this.addressList.find(x=>x.addressId == json.addressId).linkPhone;
          json.deliveryOderIdes = deliveryOrderIdList;
          console.log(json, 'this.saveData');
          let res = await confirmAddressAdjust(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: res.data,
              message: res.errMsg,
              type: 'success',
            });
            this.$emit('close', json);
          } else {
            this.saveData = null;
          }
        }
      }
    },
  },
};
</script>
